package com.ktjiaoyu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjiaoyu.crm.config.shiro.MyShiroRealm;
import com.ktjiaoyu.crm.mapper.RoleMapper;
import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.pojo.RoleRight;
import com.ktjiaoyu.crm.service.RoleRightService;
import com.ktjiaoyu.crm.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    RoleRightService roleRightService;

    @Override
    public List<Role> findAllRoles() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<Role> getRolesByPage(String roleName, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page<Role> page=new Page<Role>(pageNo,pageSize);

        //构造过滤条件
        QueryWrapper<Role> qw=new QueryWrapper<>();
        qw.like((roleName!=null && !"".equals(roleName)),
                "role_name",roleName);

        return roleMapper.selectPage(page,qw);
    }

    @Override
    public void addRole(Role role, String[] rightCodes) {
        //1.首先角色表
        roleMapper.insert(role);

        //2.保存角色拥有的权限表
        if(rightCodes!=null && rightCodes.length>0){
            for (String rightCode : rightCodes) {
                //构造角色权限对象
                RoleRight roleRight=new RoleRight();
                //设置角色id
                roleRight.setRfRoleId(role.getRoleId());
                //设置权限id
                roleRight.setRfRightCode(rightCode);

                roleRightService.addRoleRight(roleRight);
            }
        }
    }

    @Override
    public Role getRole(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void updateRole(Role role, String[] rightCodes) {
        //1.修改角色表
        roleMapper.updateById(role);
        //2.删除 该角色所拥有的权限
        roleRightService.deleteByRoleId(role.getRoleId());
        //3.添加 该角色所拥有的权限
        if(rightCodes!=null && rightCodes.length>0){
            for (String rightCode : rightCodes) {
                //构造角色权限对象
                RoleRight roleRight=new RoleRight();
                //设置角色id
                roleRight.setRfRoleId(role.getRoleId());
                //设置权限id
                roleRight.setRfRightCode(rightCode);

                roleRightService.addRoleRight(roleRight);
            }
        }

        //清空所有用户缓存
        RealmSecurityManager realmManager= (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm=(MyShiroRealm)realmManager.getRealms().iterator().next();
        realm.clearAllCacheAuthorizationInfo();
    }

    @Override
    public void deleteRole(Long roleId) {
        //1.删除该角色所用的权限
        roleRightService.deleteByRoleId(roleId);
        //2.删除角色信息
        roleMapper.deleteById(roleId);
    }

    @Override
    public Role getRoleByUsrId(Long usrId) {
        return roleMapper.getRoleByUsrId(usrId);
    }

}
